package com.app.controller.user;

import com.app.framework.common.R;
import com.app.service.user.LoginService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import me.zhyd.oauth.model.AuthCallback;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * 登录控制器
 */
@RestController
@RequestMapping("/api/auth")
public class LoginController {

    @Resource
    private LoginService loginService;

    /**
     * 第三方登录(获取跳转路径)
     */
    @GetMapping("/qq")
    public R<String> qq(HttpServletResponse response) {
        return R.ok(loginService.qq(response));
    }

    /**
     * 第三方授权
     *
     * @param callback 第三方请求携带的参数
     * @param response 后端httpServlet响应
     */
    @GetMapping("/callback/qq")
    public void callback(AuthCallback callback, HttpServletResponse response) throws IOException {
        loginService.callback(callback, response);
    }

    /**
     * 退出登录
     */
    @PostMapping("/out")
    public R<?> out() {
        loginService.out();
        return R.ok(null, "退出登录成功");
    }

    /**
     * 用于本地测试
     *
     * @param userId 用户ID
     */
    @PostMapping("/localhost/{userId}")
    public R<String> localhost(@PathVariable("userId") Long userId) {
        return R.ok(loginService.localhost(userId));
    }

}